<template>
  <div class="main">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="page" class="demo-form-inline" size="mini" style="margin-top: 20px">
      <el-form-item label="歌手">
        <el-input v-model="page.params.artist" placeholder="歌手" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" sizi="mini" @click="getByPage">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 我是分割线 -->
    <!-- <el-divider /> -->
    <el-button type="primary" size="mini" class="add-btn" style="float: left" @click="addMusic">新增</el-button>
    <audio :src="musicUrl" :autoplay="autoplay" controls="controls" style="float: right" />
    <el-table :data="page.list" border style="width: 100%" @sort-change="changeSort">
      <el-table-column prop="id" label="编号" width="100" />
      <el-table-column prop="name" label="歌曲名" width="150" show-overflow-tooltip />
      <el-table-column prop="artist" label="歌手" width="150" />
      <el-table-column prop="cover" label="封面" width="150">
        <template slot-scope="scope">
          <el-image
            style="width: 100%;height: 100px"
            :src="scope.row.cover"
            :preview-src-list="[scope.row.cover]"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createdTime" label="创建时间" width="160" sortable="custom" />
      <el-table-column prop="enable" label="启用" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enable === 1">启用</el-tag>
          <el-tag v-else type="info">未启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-dropdown>
            <el-button size="mini" type="primary">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handleRead(scope.row.id)">歌词</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="primary" @click="handleHear(scope.row.url)">试听</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="warning" @click="handleEdit(scope.row.id)">编辑</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-if="scope.row.enable === 0">
                <el-button size="mini" type="success" @click="handleEnable(scope.row.id)">启用</el-button>
              </el-dropdown-item>
              <el-dropdown-item v-else>
                <el-button size="mini" type="info" @click="handleDisable(scope.row.id)">弃用</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="float: left"
      class="pagination"
      :current-page="page.currentPage"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <div v-if="showAdd">
      <el-dialog
        title="新增"
        :visible.sync="showAdd"
      >
        <music-add @getByPage="getByPage" @closeAddForm="closeAddForm" />
      </el-dialog>
    </div>

    <div v-if="showUpdate">
      <el-dialog
        title="修改"
        :visible.sync="showUpdate"
      >
        <music-update :music="music" @getByPage="getByPage" @closeUpdateForm="closeUpdateForm" />
      </el-dialog>
    </div>

    <el-dialog
      title="查看歌词"
      :visible.sync="showRead"
      width="50%"
      overflow:auto
    >
      <el-input v-model="music.lrc" :rows="20" type="textarea" readonly />
    </el-dialog>
  </div>
</template>

<script>
import musicApi from '@/api/music'
import MusicAdd from './music-add'
import MusicUpdate from './music-update'
export default {
  components: {
    MusicAdd,
    MusicUpdate
  },
  data() {
    return {
      showAdd: false, // 控制添加博客弹窗是否打开
      showUpdate: false, // 控制分类修改弹窗是否打开
      showRead: false, // 控制阅读窗口是否打开
      musicList: [],
      music: {},
      musicUrl: '', // 音乐预览url
      autoplay: '', // 自动播放
      page: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        totalPage: 0, // 总页数
        totalCount: 0, // 总条数
        params: {}, // 查询参数对象
        list: [], // 数据
        sortColumn: '', // 排序列
        sortMethod: 'asc' // 排序方式
      }
    }
  },
  created() {
    this.getByPage()
  },
  methods: {
    getByPage() {
      this.page.list = null
      musicApi.getByPage(this.page).then(resp => {
        console.log(resp.data)
        this.page = resp.data
      })
    },
    handleRead(id) {
      musicApi.getById(id).then(resp => {
        this.music = resp.data
        this.showRead = true
      })
    },
    handleEdit(id) {
      // 修改
      musicApi.getById(id).then(resp => {
        this.music = resp.data
        this.showUpdate = true
      })
    },
    handleEnable(id) {
      this.$confirm('是否启用?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        musicApi.enable(id).then(resp => {
          this.$message.success('启用成功!')
          this.getByPage()
        })
      })
    },
    handleDisable(id) {
      this.$confirm('是否弃用?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        musicApi.disable(id).then(resp => {
          this.$message.success('弃用成功!')
          this.getByPage()
        })
      })
    },
    handleDelete(id) {
      // 删除
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        musicApi.deleteById(id)
        this.$message.success('删除成功!')
        this.getByPage()
      })
    },
    addMusic() {
      this.showAdd = true
    },
    closeAddForm() {
      this.showAdd = false
    },
    closeUpdateForm() {
      this.showUpdate = false
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getByPage()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.getByPage()
    },
    changeSort(e) {
      if (e.order) {
        this.page.sortColumn = e.prop
        this.page.sortMethod = e.order
      } else {
        this.page.sortColumn = ''
        this.page.sortMethod = 'asc'
      }
      this.getByPage()
    },
    handleHear(url) {
      this.musicUrl = url
      this.autoplay = true
    }
  }
}
</script>
<style scoped>
  .add-btn {
    margin: 5px 5px 5px 0px;
  }
  .add-container {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
</style>
